﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOM.Common;
using Subgurim.Controles;

public partial class Controles_ReclamacaoLocalizacao : System.Web.UI.UserControl
{

    //Latitude da posicao atual marcada no mapa
    public double? Latitude
    {
        get
        {
            double? latitude = null;
            if (!string.IsNullOrEmpty(txtLatitude.Text))
            {
                latitude = Convert.ToDouble(txtLatitude.Text);
            }
            return latitude;
        }
    }

    //Longitude da posicao atual marcada no mapa
    public double? Longitude
    {
        get
        {
            double? longitude = null;
            if (!string.IsNullOrEmpty(txtLongitude.Text))
            {
                longitude = Convert.ToDouble(txtLongitude.Text);
            }
            return longitude;

        }
    }

    private TipoOperacao TipoOperacao
    {
        get
        {
            if (Request["op"] == null)
            {
                throw new InvalidOperationException("Operação Inválida");
            }
            
            return (TipoOperacao)Convert.ToInt32(Request["op"]);
        }
    }

    public GMap Mapa
    {
        get
        {
            return this.GMap1;
        }

        set
        {
            GMap1 = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            GMap1.getChangeHiddenStateFunction();

            if (TipoOperacao == TipoOperacao.Visualizacao)
            {
                GMap1.GZoom = 15;
                GMap1.enableGoogleBar = false;
                GMap1.addControl(new GControl(new CarouselMapTypeControl()));
                GMap1.addControl(new GControl(GControl.preBuilt.SmallZoomControl3D));
                GMap1.addControl(new GControl(GControl.extraBuilt.MarkCenter));
                GMap1.addControl(new GControl(GControl.extraBuilt.TextualCoordinatesControl, new GControlPosition(GControlPosition.position.Bottom_Right)));
                GMap1.enableHookMouseWheelToZoom = true;
                GMap1.enableGKeyboardHandler = true;
                GMap1.enableDoubleClickZoom = true;
                return;
            }

            if (TipoOperacao == TipoOperacao.Cadastro)
                GMap1.setCenter(Common.PosicaoPadrao); //Centro em Salvador


            GMap1.addControl(new GControl(GControl.extraBuilt.MarkCenter));
            GMap1.addControl(new GControl(GControl.preBuilt.MapTypeControl));
            GMap1.addControl(new GControl(GControl.preBuilt.SmallZoomControl3D,new GControlPosition(GControlPosition.position.Top_Left)));
            GMap1.addControl(new GControl(GControl.extraBuilt.TextualCoordinatesControl, new GControlPosition(GControlPosition.position.Bottom_Right)));
            GMap1.enableHookMouseWheelToZoom = true;
            GMap1.enableGKeyboardHandler = true;
            GMap1.enableGoogleBar = true;
            GMap1.enableDoubleClickZoom = true;



        }
    }

    public string getChangeHiddenStateFunction()
    {
        return this.GMap1.getChangeHiddenStateFunction();
    }

    protected void btnMarcar_Click(object sender, EventArgs e)
    {
        try
        {
            GLatLng gLatLng = new GLatLng();

            if (string.IsNullOrEmpty(txtLatitude.Text) ||
                string.IsNullOrEmpty(txtLongitude.Text))
            {
                gLatLng = GMap1.GCenter;
            }
            else
            {
                gLatLng.lat = Convert.ToDouble(txtLatitude.Text);
                gLatLng.lng = Convert.ToDouble(txtLongitude.Text);
            }
            GIcon gIcon = new GIcon();
            gIcon.markerIconOptions = new MarkerIconOptions(50, 50, Color.Red);
            GMarker gMarker = new GMarker(gLatLng, gIcon);
            gMarker.options.clickable = false;
            gMarker.options.draggable = true;
            GInfoWindow gInfoWindow = new GInfoWindow(gMarker, gLatLng.ToString());
            GMap1.setCenter(gLatLng);
            GMap1.Add(gInfoWindow);
            btnMarcar.Enabled = false;

            txtLatitude.Text = GMap1.GCenter.lat.ToString();
            txtLongitude.Text = GMap1.GCenter.lng.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message + Environment.NewLine + ex.InnerException);
        }
    }

    protected string GMap1_Click(object s, GAjaxServerEventArgs e)
    {
        this.GMap1.setCenter(new GLatLng(e.point.lat, e.point.lng));

        return string.Empty;

    }

    protected void btnDesmarcar_Click(object sender, EventArgs e)
    {
        GMap1.resetInfoWindows();
        GMap1.resetMarkers();
        btnMarcar.Enabled = true;
        txtLatitude.Text = string.Empty;
        txtLongitude.Text = string.Empty;
    }

    public void MarcarPosicao(double latitude, double longitude)
    {
        txtLatitude.Text = latitude.ToString();
        txtLongitude.Text = longitude.ToString();

        GLatLng gLand = new GLatLng(latitude, longitude);
        GIcon gIcon = new GIcon();
        gIcon.markerIconOptions = new MarkerIconOptions(50, 50, Color.Red);

        GMarker gMarker = new GMarker(gLand, gIcon);
        GInfoWindow gInfoWindow = new GInfoWindow(gMarker, gLand.ToString());
        GMap1.Add(gInfoWindow);
        GMap1.setCenter(gLand);
        btnMarcar.Enabled = false;
    }

    public void DesativarControlesEdicao()
    {
        frmControlesEdicao.Visible = false;
    }

    public void LimparControles()
    {
        txtLatitude.Text = string.Empty;
        txtLongitude.Text = string.Empty;

        GMap1.resetInfoWindows();
        btnMarcar.Enabled = true;
    }

}
